import { login, logout, getInfo, getDingInfo } from "@/api/login";
import { getToken, setToken, removeToken } from "@/utils/auth";
import { Message } from "element-ui";
const user = {
  state: {
    token: getToken(),
    name: "",
    avatar: "",
    nickName: "",
    roles: [],
    permissions: [],
    id: "",
    phonenumber: "",
    deptId: "",
    deptType: "",
    deptName: "",
    deptparentId: "", // 父部门ID
    deptparentName: "",
    deptLevel: 0, // 部门级别 0 最低级 1 中间级 2最高级
    administrativeDivision: "",
    administrativeDivisionName: '',
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_NAME: (state, name) => {
      state.name = name;
    },
    SET_NickName: (state, name) => {
      state.nickName = name;
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles;
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions;
    },
    SET_ID: (state, id) => {
      state.id = id;
    },
    SET_PHONENUMBER: (state, phonenumber) => {
      state.phonenumber = phonenumber;
    },
    SET_DEPTID: (state, deptId) => {
      state.deptId = deptId;
    },
    SET_DEPTTYPE: (state, deptType) => {
      state.deptType = deptType;
    },
    SET_DEPNAME: (state, deptName) => {
      state.deptName = deptName;
    },
    SET_PARENTID: (state, deptparentId) => {
      state.deptparentId = deptparentId;
    },
    SET_PARENTNAME: (state, deptparentName) => {
      state.deptparentName = deptparentName;
    },
    SET_DEPTLEVEL: (state, deptLevel) => {
      state.deptLevel = deptLevel;
    },
    SET_ADMINISTRATIVEDIVISION: (state, administrativeDivision) => {
      state.administrativeDivision = administrativeDivision;
    },
    SET_ADMINISTRATIVEDIVISIONNAME: (state, administrativeDivisionName) => {
      state.administrativeDivisionName = administrativeDivisionName;
    },
  },

  actions: {
    // 浙政钉登录
    DingLogin({ commit }, params) {
      return new Promise((resolve, reject) => {
        getDingInfo(params)
          .then((res) => {
            if (res.code === 700) {
              // this.$message.warning(res.msg);
              // return false;
            } else {
              setToken(res.token);
              commit("SET_TOKEN", res.token);
            }
            resolve(res);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim();
      const password = userInfo.password;
      const code = userInfo.code;
      const uuid = userInfo.uuid;
      return new Promise((resolve, reject) => {
        login(username, password, code, uuid)
          .then((res) => {
            if (res.code === 700) {
              Message.warning(res.msg);
              reject();
              return false;
            }
            setToken(res.token);
            commit("SET_TOKEN", res.token);
            resolve(res);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo()
          .then((res) => {
            console.log(res, "登录用户信息");
            if (res.code !== 200) {
              resolve(res);
              return false;
            }
            const user = res.user;
            const avatar =
              user.avatar == "" || user.avatar == null
                ? require("@/assets/images/login/user.png")
                : process.env.VUE_APP_BASE_API + user.avatar;
            if (res.roles && res.roles.length > 0) {
              // 验证返回的roles是否是一个非空数组
              commit("SET_ROLES", res.roles);
              commit("SET_PERMISSIONS", res.permissions);
            } else {
              commit("SET_ROLES", ["ROLE_DEFAULT"]);
            }
            commit("SET_NAME", user.userName);
            commit("SET_NickName", user.nickName);
            commit("SET_AVATAR", avatar);
            commit("SET_ID", user.userId);
            commit("SET_PHONENUMBER", user.phonenumber);
            commit("SET_DEPTID", user.deptId);
            commit("SET_DEPTTYPE", user.dept.type);
            commit("SET_DEPTLEVEL", user.deptLevel);
            commit("SET_DEPNAME", user.dept.deptName);
            commit("SET_PARENTID", user.dept.parentId);
            commit("SET_PARENTNAME", user.dept.parentName);
            commit(
              "SET_ADMINISTRATIVEDIVISION",
              user.dept.administrativeDivision
            );
            commit(
              "SET_ADMINISTRATIVEDIVISIONNAME",
              user.dept.administrativeDivisionName
            );
            resolve(res);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token)
          .then(() => {
            commit("SET_TOKEN", "");
            commit("SET_ROLES", []);
            commit("SET_PERMISSIONS", []);
            commit("SET_ID", "");
            removeToken();
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise((resolve) => {
        commit("SET_TOKEN", "");
        removeToken();
        resolve();
      });
    },
  },
};

export default user;
